'use strict';
const Controller = require('egg').Controller;
const { v4: uuidv4 } = require('uuid');

class HomeController extends Controller {
  async index() {
    const schoolId = uuidv4();
    const shopId = uuidv4();
    this.ctx.body = {
      code: 200,
      msg: '',
      status: 'success',
      data: {
        schoolId,
        shopId
      }
    }

  }

  async login() {
    const { ctx, app } = this;
    const { openId, nickName, avatarUrl } = ctx.request.body;
    //调用service
    const result = await ctx.service.home.login(openId, nickName, avatarUrl);
    ctx.body = result;
  }

  async submitOrder() {
    const { ctx, app } = this;
    const { open_id } = ctx.params;
    const parameter = ctx.request.body;
    const result = await ctx.service.home.submitOrder(open_id, parameter);
    ctx.body = result;
  }

  async getSimpleOrders() {
    const { ctx, app } = this;
    const { open_id } = ctx.params;
    let { page, limit } = ctx.query;
    const result = await ctx.service.home.getSimpleOrders(open_id, page, limit);
    ctx.body = result;

  }

  async getDetailOrder() {
    const { ctx, app } = this;
    const { order_id } = ctx.params;
    const result = await ctx.service.home.getDetailOrder(order_id);
    ctx.body = result;
  }

  async getAddress() {
    const { ctx, app } = this;
    const { open_id } = ctx.params;
    const { page, limit } = ctx.query;
    const result = await ctx.service.home.getAddress(open_id, page, limit);
    ctx.body = result;
  }
  
  async getAddressDetail() {
    const { ctx, app } = this;
    const { address_id } = ctx.params;
    const result = await ctx.service.home.getAddressDetail(address_id);
    ctx.body = result;
  }

  async increaseAddress() {
    const { ctx, app } = this;
    const { open_id } = ctx.params;
    const data = ctx.request.body;
    const result = await ctx.service.home.increaseAddress(open_id, data);
    ctx.body = result;
  }

  async updateAddress() {
    const { ctx, app } = this;
    const { address_id } = ctx.params;
    const data = ctx.request.body;
    const result = await ctx.service.home.updateAddress(address_id, data);
    ctx.body = result;
  }

  async deleteAddress() {
    const { ctx, app } = this;
    const { address_id } = ctx.params;
    const result = await ctx.service.home.deleteAddress(address_id);
    ctx.body = result;
  }

  async setDefaultAddress() {
    const { ctx, app } = this;
    const { address_id, open_id } = ctx.request.body;
    const result = await ctx.service.home.setDefaultAddress(address_id, open_id);
    ctx.body = result;
  }

  async getPickerDatas() {
    const { ctx, app } = this;
    const result = await ctx.service.home.getPickerDatas();
    ctx.body = result;
  }

  async updateSelectSchool() {
    const { ctx, app } = this;
    const { open_id, school_id } = ctx.request.body;
    const result = await ctx.service.home.updateSelectSchool(open_id, school_id);
    ctx.body = result;

  }

  async getUserInfo() {
    const { ctx, app } = this;
    const { open_id } = ctx.params;
    const result = await ctx.service.home.getUserInfo(open_id);
    ctx.body = result;
  }

  async getShopBySchoolId() {
    const { ctx, app } = this;
    const { school_id } = ctx.params;
    const result = await ctx.service.home.getShopBySchoolId(school_id);
    ctx.body = result;
  }

  async getSelectSchoolInfo() {
    const { ctx, app } = this;
    const { school_id } = ctx.params;
    const result = await ctx.service.home.getSelectSchoolInfo(school_id);
    ctx.body = result;
  }

}

module.exports = HomeController;
